﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<style type="text/css">
    .etable td {
        border: 1px solid grey;
    }

    .ml-1 {
        margin-left: 10px;
    }

    .ml-2 {
        margin-left: 20px;
    }

    .ml-3 {
        margin-left: 30px;
    }

    .w-50 {
        width: 50px;
    }

    .w-100 {
        width: 100px;
    }

    #dataDiv {
        height: 500px;
    }

    .data {
        width: 48%;
        height: 48%;
        text-align: center;
        display: inline-block
    }

        .data textarea {
            width: 600px;
            height: 600px;
        }

    #sqlDiv {
        float: left;
    }

</style>

<div class="main">
    <div id="headDiv">
        <input id="hide_sqlTypeOptions" type="hidden" value="@ViewData["sqlTypeOptions"]" />
        <input id="hide_ormTypeOptions" type="hidden" value="@ViewData["ormTypeOptions"]" />
        <input id="hide_scanTypeOptions" type="hidden" value="@ViewData["scanTypeOptions"]" />
        <h3>生成 C# 实体类工具  <button class="btn btn-primary ml-2" onclick="Save()">生成实体</button></h3>
        <p>
            <label>配置：</label>
            <label class="ml-2">
                实体类名：<input type="text" style="width:150px;" id="txt_TableName"/>
            </label>
            <label class="ml-2">
                Sql表名：<input type="text" style="width:150px;" id="txt_SqlTableName"/>
            </label>
            <label class="ml-2">
                表注释：<input type="text" style="width:200px;" id="txt_TableRemark" />
            </label>
            <label class="ml-2">
                添加ORM特性：
                <select id="sel_WithOrmAttribute" style="width:80px;">
                    <option value="True">是</option>
                    <option value="False">否</option>
                </select>
            </label>
            <label class="ml-2">
                ORM类型：
                <select id="sel_OrmType" style="width:100px;height:30px;text-align:center;">
                </select>
            </label>
        </p>
        <p>
            <label>其它：</label>
            <label class="ml-2">
                sql字段前缀：
                <input type="text" id="txt_SqlFieldPreFix" style="width:80px;" />
            </label>
            <label class="ml-2">
                c#带默认值：
                <select id="sel_CsEntityWithDefalueValue" style="width:50px;">
                    <option value="True">是</option>
                    <option value="False">否</option>
                </select>
            </label>
            <label class="ml-2">
                sql字段分割字母:
                <select id="sel_SqlFieldSplitWords" style="width:50px;">
                    <option value="False">否</option>
                    <option value="True">是</option>
                </select>
            </label>
            <label class="ml-2">
                sql字段转小写:
                <select id="sel_SqlFieldLowerCase" style="width:50px;">
                    <option value="False">否</option>
                    <option value="True">是</option>
                </select>
            </label>
            <label class="ml-2">
                实体基类名：<input type="text" id="txt_BaseEntityName" style="width:100px;" value="" />
            </label>
            <label class="ml-2">
                基类带基础字段：
                <select id="sel_WithBaseFields" style="width:50px;">
                    <option value="False">否</option>
                    <option value="True">是</option>
                </select>
            </label>
        </p>
        <p>
            <label>拓展：</label>
            <label class="ml-2">
                业务场景：
                <select id="sel_ScanType" style="width:150px;height:30px;text-align:center;">
                </select>
            </label>
        </p>
    </div>
    <div id="tableDiv">
        <table id="table" class="etable table table-hover table-border">
            <tr style="background-color:#BBBBB8" id="0">
                <td>字段名</td>
                <td>数据类型</td>
                <td>是否主键</td>
                <td>长度</td>
                <td>是否允许空</td>
                <td>是否是标识列</td>
                <td>注释</td>
                <td><button style="color:red" onclick="AddRow()">＋</button></td>
            </tr>
        </table>
    </div>
    <div id="dataDiv">
        <div class="data" id="sqlDiv" style="float:left;">
            <h3>SQL建表语句</h3>
            <textarea wrap="off" id="sqlData"></textarea>
        </div>
        <div class="data" id="ormDiv">
            <h3>C#实体类</h3>
            <textarea wrap="off" id="ormData"></textarea>
        </div>
    </div>
</div>


<script src="~/lib/jquery/dist/jquery.min.js"></script>

<script type="text/javascript">
    var rowId = 0;
    function AddRow(beforeId){
        rowId++;
        var dataTrHtml = '<tr class="dataTr" id="'+rowId+'">';
        dataTrHtml+='<td><input type="text" id="txt_FieldName_'+rowId+'" /></td>';
        dataTrHtml+='<td><select class="selDataType" id="sel_DataType_'+rowId+'"></select></td>';
        dataTrHtml+='<td><input type="radio" id="rdo_IsPrimaryKey_'+rowId+'_false" name="rdo_IsPrimaryKey_'+rowId+'" checked value="False" />否<input type="radio" value="True" id="rdo_IsPrimaryKey_'+rowId+'_true" name="rdo_IsPrimaryKey_'+rowId+'" />是</td>';
        dataTrHtml+='<td><input type="text" id="txt_Length_'+rowId+'" /></td>';
        dataTrHtml+='<td><input type="radio" id="rdo_IsNullable_'+rowId+'_false" checked value="False" name="rdo_IsNullable_'+rowId+'" />否<input type="radio" value="True" id="rdo_IsNullable_'+rowId+'_true" name="rdo_IsNullable_'+rowId+'" />是</td>';
        dataTrHtml+='<td><input type="radio" id="rdo_IsIdentity_'+rowId+'_false" checked value="False" name="rdo_IsIdentity_'+rowId+'" />否<input type="radio" value="True" id="rdo_IsIdentity_'+rowId+'_true" name="rdo_IsIdentity_'+rowId+'" />是</td>';
        dataTrHtml+='<td><input type="text" id="txt_Remark_'+rowId+'" /></td>';
        dataTrHtml+='<td><button style="color:blue" onclick=AddRow(' + rowId + ')>+</button>&nbsp;&nbsp; <button style="color:red" onclick=DelRow('+rowId+')>-</button></td>';
        if (beforeId == undefined || beforeId == null) {
            $("#table").append(dataTrHtml);
        } else {
            $("#" + beforeId).after(dataTrHtml);
        }
        BindSelect($("#sel_DataType_"+rowId));
    }
</script>

<script type="text/javascript">
    $(function(){
        $("#sel_OrmType").append($("#hide_ormTypeOptions").val());
        $("#sel_ScanType").append($("#hide_scanTypeOptions").val());
        $("#dataDiv").hide();
        AddRow();

        $("#sel_ScanType").change(function(){
            if($(this).val()=="0"){
                $("#sel_SqlFieldSplitWords").val("False");
                $("#sel_SqlFieldLowerCase").val("False");
                $("#txt_BaseEntityName").val("");
            }else{
                $("#sel_SqlFieldSplitWords").val("True");
                $("#sel_SqlFieldLowerCase").val("True");
                $("#txt_BaseEntityName").val("IEntity");
            }
        })
    })

    function BindSelect(item){
        var options = $("#hide_sqlTypeOptions").val();
        $(item).append(options);
    }

    function DelRow(rowId){
        $("#"+rowId).remove();
    }

    function Save(){
        var trs = $(".dataTr");
        var TableName=$("#txt_TableName").val();
        if(TableName==""){
            alert("表名不可为空！");
            return;
        }

        var tableInfo = {
            "TableName":TableName,
            "TableRemark":$("#txt_TableRemark").val(),
            "SqlTableName":$("#txt_SqlTableName").val(),
            "SqlFieldPreFix":$("#txt_SqlFieldPreFix").val(),
            "WithOrmAttribute":$("#sel_WithOrmAttribute").val(),
            "CsEntityWithDefalueValue":$("#sel_CsEntityWithDefalueValue").val(),
            "BaseEntityName":$("#txt_BaseEntityName").val(),
            "SqlFieldSplitWords":$("#sel_SqlFieldSplitWords").val(),
            "SqlFieldLowerCase":$("#sel_SqlFieldLowerCase").val(),
            "ScanType":$("#sel_ScanType").val(),
            "OrmType":$("#sel_OrmType").val(),
            "WithBaseFields":$("#sel_WithBaseFields").val(),
            "Fields":[]
        }

        $.each(trs,function(i,item){
            var rowId=item.id;
            var field = {
                "FieldName":$("#txt_FieldName_"+rowId).val(),
                "DataType":$("#sel_DataType_"+rowId).val(),
                "IsPrimaryKey":$("input[name='rdo_IsPrimaryKey_"+rowId+"']:checked").val(),
                "Length":$("#txt_Length_"+rowId).val(),
                "IsNullable":$("input[name='rdo_IsNullable_"+rowId+"']:checked").val(),
                "IsIdentity":$("input[name='rdo_IsIdentity_"+rowId+"']:checked").val(),
                "Remark":$("#txt_Remark_"+rowId).val()
            };
            tableInfo.Fields.push(field);
        });
        $.ajax({
                url: "/WebTools/BuildData",
                data: {
                table:tableInfo
                },
                type: 'POST',
                dataType: 'json',
                contentType: "application/x-www-form-urlencoded",
                cache: false,
                async: false,
                success: function (result) {
                    if (result.code==0) {
                        if(result.data){
                            $("#dataDiv").show();
                            $("#sqlData").val(result.data.sqlData);
                            $("#ormData").val(result.data.ormData);  
                        }
                    }else{
                        alert(result.message);
                    }
                }
            });

    }
</script>  